<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="企业名称" prop="enterpriseName">
                <el-input v-model="queryParams.enterpriseName" placeholder="请输入企业名称" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="统一社会信用代码" label-width="130px" prop="creditCode">
                <el-input v-model="queryParams.creditCode" placeholder="请输入统一社会信用代码" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="实名状态" label-width="80px" prop="realNameFlag">
                <el-select class="w-30" v-model="queryParams.realNameFlag" placeholder="请选择实名状态" clearable>
                    <el-option label="已实名" value="1" />
                    <el-option label="未实名" value="0" />
                </el-select>
            </el-form-item>
            <el-form-item label="注册时间" label-width="70px" prop="createTimeRange">
                <el-date-picker v-model="queryParams.createTimeRange" type="daterange" range-separator="至"
                    start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" clearable
                    @change="handleQuery" />
            </el-form-item>
            <el-form-item label="企业所属地" prop="provinceName">

                <CityChoose class="!w-100" v-model:provinceSiteId="queryParams.provinceSiteId"
                    v-model:citySiteId="queryParams.citySiteId" v-model:areaSiteId="queryParams.areaSiteId" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>



        <!-- <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['manage:enterprise:export']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->
        <el-tabs v-model="queryParams.examineResult" @tab-change="handleTabChange">
            <el-tab-pane label="待审核" name="0"></el-tab-pane>
            <el-tab-pane label="全部" name=""></el-tab-pane>
            <el-tab-pane label="审核通过" name="2"></el-tab-pane>
            <el-tab-pane label="审核未通过" name="1"></el-tab-pane>
        </el-tabs>

        <el-table v-loading="loading" border :data="enterpriseList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="id" align="center" prop="id" /> -->
            <el-table-column show-overflow-tooltip fixed="left" width="300" label="企业名称" align="center"
                prop="enterpriseName" />
            <el-table-column show-overflow-tooltip width="140" label="统一社会信用代码" align="center" prop="creditCode" />
            <el-table-column show-overflow-tooltip width="140" label="法人" align="center" prop="enterpriseName" />
            <el-table-column show-overflow-tooltip width="140" label="法人电话" align="center" prop="enterpriseName" />
            <el-table-column label="审核状态" align="center" width="100" prop="examineResult"
                :formatter="formatExamineResult" />
            <el-table-column label="实名状态" align="center" prop="realNameFlag">
                <template #default="scope">
                    <span>{{ scope.row.realNameFlag === '1' ? '已实名' : '未实名' }}</span>
                </template>
            </el-table-column>
            <el-table-column label="认证状态 " align="center" prop="authenticationFlag">
                <template #default="scope">
                    <span>{{ scope.row.authenticationFlag === '1' ? '已认证' : '未认证' }}</span>
                </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip width="100" label="来源" align="center" prop="source">
                <template #default="scope">
                    企业PC
                </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip width="100" label="来源渠道" align="center" prop="sourceChannel"
                :formatter="(row) => row.sourceChannel == '2' ? '运营导入' : '自然流量'" />
            <el-table-column show-overflow-tooltip width="140" label="注册时间" align="center" prop="createTime" />
            <el-table-column show-overflow-tooltip width="140" label="企业所属地省份" align="center" prop="provinceName" />
            <el-table-column show-overflow-tooltip width="140" label="企业所属地城市" align="center" prop="cityName" />
            <el-table-column show-overflow-tooltip width="140" label="企业所属地区县" align="center" prop="areaName" />
            <el-table-column label="操作" width="200" fixed="right" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="view" @click="handleView(scope.row)"
                        v-hasPermi="['manage:enterprise:edit']">查看</el-button>
                    <el-button link type="primary" v-if="scope.row.examineResult === '0' && scope.row.authenticationFlag === '1'" icon="Edit"
                        @click="handleUpdate(scope.row)" v-hasPermi="['manage:enterprise:edit']">审核</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改企业管理对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="enterpriseRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="企业名称" prop="enterpriseName">
                    <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" />
                </el-form-item>
                <el-form-item label="统一社会信用代码" prop="creditCode">
                    <el-input v-model="form.creditCode" placeholder="请输入统一社会信用代码" />
                </el-form-item>
                <el-form-item label="企业类型" prop="enterpriseNature">
                    <el-input v-model="form.enterpriseNature" placeholder="请输入企业类型" />
                </el-form-item>
                <el-form-item label="注册地址" prop="legalAddress">
                    <el-input v-model="form.legalAddress" placeholder="请输入注册地址" />
                </el-form-item>
                <el-form-item label="通讯地址" prop="corrAddress">
                    <el-input v-model="form.corrAddress" placeholder="请输入通讯地址" />
                </el-form-item>
                <el-form-item label="所属行业" prop="industry">
                    <el-input v-model="form.industry" placeholder="请输入所属行业" />
                </el-form-item>
                <el-form-item label="来源" prop="source">
                    <el-input v-model="form.source" placeholder="请输入来源" />
                </el-form-item>
                <el-form-item label="来源渠道" prop="sourceChannel">
                    <el-input v-model="form.sourceChannel" placeholder="请输入来源渠道" />
                </el-form-item>
                <el-form-item label="小微企业" prop="microFlag">
                    <el-input v-model="form.microFlag" placeholder="请输入小微企业" />
                </el-form-item>
                <el-form-item label="连续纳税年限" prop="taxYears">
                    <el-input v-model="form.taxYears" placeholder="请输入连续纳税年限" />
                </el-form-item>
                <el-form-item label="企业标签" prop="enterpriseLabel">
                    <el-input v-model="form.enterpriseLabel" placeholder="请输入企业标签" />
                </el-form-item>
                <el-form-item label="企业人数" prop="staffNum">
                    <el-input v-model="form.staffNum" placeholder="请输入企业人数" />
                </el-form-item>
                <el-form-item label="上级用户" prop="superiorUser">
                    <el-input v-model="form.superiorUser" placeholder="请输入上级用户" />
                </el-form-item>
                <el-form-item label="红黑名单" prop="redBlackList">
                    <el-input v-model="form.redBlackList" placeholder="请输入红黑名单" />
                </el-form-item>
                <el-form-item label="营业期限自" prop="businessTermStart">
                    <el-date-picker clearable v-model="form.businessTermStart" type="date" value-format="YYYY-MM-DD"
                        placeholder="请选择营业期限自">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="营业期限止" prop="businessTermEnd">
                    <el-date-picker clearable v-model="form.businessTermEnd" type="date" value-format="YYYY-MM-DD"
                        placeholder="请选择营业期限止">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="经营范围" prop="businessScope">
                    <el-input v-model="form.businessScope" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="法人认证标识" prop="legalaAuthFlag">
                    <el-input v-model="form.legalaAuthFlag" placeholder="请输入法人认证标识" />
                </el-form-item>
                <el-form-item label="经办人认证标识" prop="agentAuthFlag">
                    <el-input v-model="form.agentAuthFlag" placeholder="请输入经办人认证标识" />
                </el-form-item>
                <el-form-item label="认证状态 " prop="authenticationFlag">
                    <el-input v-model="form.authenticationFlag" placeholder="请输入认证状态 " />
                </el-form-item>
                <el-form-item label="认证时间" prop="authenticationTime">
                    <el-date-picker clearable v-model="form.authenticationTime" type="date" value-format="YYYY-MM-DD"
                        placeholder="请选择认证时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="企业成立日期" prop="registrationDate">
                    <el-date-picker clearable v-model="form.registrationDate" type="date" value-format="YYYY-MM-DD"
                        placeholder="请选择企业成立日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="企业实名状态" prop="realNameFlag">
                    <el-input v-model="form.realNameFlag" placeholder="请输入企业实名状态" />
                </el-form-item>
                <el-form-item label="企业实名来源" prop="realNameSource">
                    <el-input v-model="form.realNameSource" placeholder="请输入企业实名来源" />
                </el-form-item>
                <el-form-item label="签章状态" prop="changeRule">
                    <el-input v-model="form.changeRule" placeholder="请输入签章状态" />
                </el-form-item>
                <el-form-item label="审核结果" prop="examineResult">
                    <el-input v-model="form.examineResult" placeholder="请输入审核结果" />
                </el-form-item>
                <el-form-item label="企业基本户开户行" prop="depositBank">
                    <el-input v-model="form.depositBank" placeholder="请输入企业基本户开户行" />
                </el-form-item>
                <el-form-item label="企业基本户开户行-其他" prop="depositBankOther">
                    <el-input v-model="form.depositBankOther" placeholder="请输入企业基本户开户行-其他" />
                </el-form-item>
                <el-form-item label="银行账户" prop="bankAccount">
                    <el-input v-model="form.bankAccount" placeholder="请输入银行账户" />
                </el-form-item>
                <el-form-item label="融资需求笔数" prop="financingNum">
                    <el-input v-model="form.financingNum" placeholder="请输入融资需求笔数" />
                </el-form-item>
                <el-form-item label="版本" prop="version">
                    <el-input v-model="form.version" placeholder="请输入版本" />
                </el-form-item>
                <el-form-item label="删除标识" prop="deleted">
                    <el-input v-model="form.deleted" placeholder="请输入删除标识" />
                </el-form-item>
                <el-form-item label="省" prop="provinceName">
                    <el-input v-model="form.provinceName" placeholder="请输入省" />
                </el-form-item>
                <el-form-item label="市" prop="cityName">
                    <el-input v-model="form.cityName" placeholder="请输入市" />
                </el-form-item>
                <el-form-item label="区" prop="areaName">
                    <el-input v-model="form.areaName" placeholder="请输入区" />
                </el-form-item>
                <el-form-item label="经营所在省编码" prop="corrProvinceSiteId">
                    <el-input v-model="form.corrProvinceSiteId" placeholder="请输入经营所在省编码" />
                </el-form-item>
                <el-form-item label="经营所在地省名称" prop="corrProvinceSiteName">
                    <el-input v-model="form.corrProvinceSiteName" placeholder="请输入经营所在地省名称" />
                </el-form-item>
                <el-form-item label="经营所在地市编码" prop="corrCitySiteId">
                    <el-input v-model="form.corrCitySiteId" placeholder="请输入经营所在地市编码" />
                </el-form-item>
                <el-form-item label="经营所在地市名称" prop="corrCitySiteName">
                    <el-input v-model="form.corrCitySiteName" placeholder="请输入经营所在地市名称" />
                </el-form-item>
                <el-form-item label="经营所在地区编码" prop="corrAreaSiteId">
                    <el-input v-model="form.corrAreaSiteId" placeholder="请输入经营所在地区编码" />
                </el-form-item>
                <el-form-item label="经营所在地区名称" prop="corrAreaSiteName">
                    <el-input v-model="form.corrAreaSiteName" placeholder="请输入经营所在地区名称" />
                </el-form-item>
                <el-form-item label="注册资本【万元】" prop="registeredCapital">
                    <el-input v-model="form.registeredCapital" placeholder="请输入注册资本【万元】" />
                </el-form-item>
                <el-form-item label="持续经营年限" prop="durationManagement">
                    <el-input v-model="form.durationManagement" placeholder="请输入持续经营年限" />
                </el-form-item>
                <el-form-item label="单位电话" prop="workTelephone">
                    <el-input v-model="form.workTelephone" placeholder="请输入单位电话" />
                </el-form-item>
                <el-form-item label="营业场所性质" prop="businessPlace">
                    <el-input v-model="form.businessPlace" placeholder="请输入营业场所性质" />
                </el-form-item>
                <el-form-item label="纳税等级" prop="taxLevel">
                    <el-input v-model="form.taxLevel" placeholder="请输入纳税等级" />
                </el-form-item>
                <el-form-item label="核验表id" prop="checkId">
                    <el-input v-model="form.checkId" placeholder="请输入核验表id" />
                </el-form-item>
                <el-form-item label="报关成交额" prop="declareDeal">
                    <el-input v-model="form.declareDeal" placeholder="请输入报关成交额" />
                </el-form-item>
                <el-form-item label="行业名称" prop="industryName">
                    <el-input v-model="form.industryName" placeholder="请输入行业名称" />
                </el-form-item>
                <el-form-item label="站点ID" prop="siteId">
                    <el-input v-model="form.siteId" placeholder="请输入站点ID" />
                </el-form-item>
                <el-form-item label="本地站点编号" prop="siteCode">
                    <el-input v-model="form.siteCode" placeholder="请输入本地站点编号" />
                </el-form-item>
                <el-form-item label="站点名称" prop="siteName">
                    <el-input v-model="form.siteName" placeholder="请输入站点名称" />
                </el-form-item>
                <el-form-item label="认证类型" prop="authFlag">
                    <el-input v-model="form.authFlag" placeholder="请输入认证类型" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Enterprise">
import { listEnterprise, getEnterprise, delEnterprise, addEnterprise, updateEnterprise } from "@/api/manage/enterprise"
import { useDict, transformDict } from "@/hooks/dict";
import { cloneDeep } from 'lodash-es'
// const industryOptions = useDict('111'); 
const options = useDict('111')
import CityChoose from '@/components/CityChoose/index.vue'


const { proxy } = getCurrentInstance()

const enterpriseList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const activeTab = ref('0')

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        creditCode: null,
        examineResult: "0"
    },
    rules: {
    }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询企业管理列表 */
function getList() {
    loading.value = true
    const query = cloneDeep(queryParams.value)
    // 如果是待审核状态，增加新的参数
    if (query.examineResult === '0') {
        query.authenticationFlag = '1'
    }

    listEnterprise(query).then(response => {
        enterpriseList.value = response.rows
        total.value = response.total
        loading.value = false
    })
}

// 取消按钮
function cancel() {
    open.value = false
    reset()
}


// 
function handleTabChange() {
    getList()
}

function formatExamineResult(row, column) {
    const result = row.examineResult
    if (result === '0') {
        return '未审核'
    } else if (result === '2') {
        return '审核通过'
    } else if (result === '1') {
        return '审核失败'
    }
    return '未审核'

}

// 表单重置
function reset() {
    form.value = {
        id: null,
        enterpriseName: null,
        creditCode: null,
        enterpriseNature: null,
        legalAddress: null,
        corrAddress: null,
        provinceSiteId: null,
        citySiteId: null,
        areaSiteId: null,
        industry: null,
        source: null,
        sourceChannel: null,
        microFlag: null,
        taxYears: null,
        enterpriseLabel: null,
        staffNum: null,
        superiorUser: null,
        redBlackList: null,
        businessTermType: null,
        businessTermStart: null,
        businessTermEnd: null,
        businessScope: null,
        legalaAuthFlag: null,
        agentAuthFlag: null,
        authenticationFlag: null,
        authenticationTime: null,
        registrationDate: null,
        realNameFlag: null,
        realNameSource: null,
        changeRule: null,
        examineResult: null,
        depositBank: null,
        depositBankOther: null,
        bankAccount: null,
        financingNum: null,
        createTime: null,
        updateTime: null,
        version: null,
        deleted: null,
        provinceName: null,
        cityName: null,
        areaName: null,
        corrProvinceSiteId: null,
        corrProvinceSiteName: null,
        corrCitySiteId: null,
        corrCitySiteName: null,
        corrAreaSiteId: null,
        corrAreaSiteName: null,
        registeredCapital: null,
        durationManagement: null,
        workTelephone: null,
        businessPlace: null,
        taxLevel: null,
        checkId: null,
        declareDeal: null,
        industryName: null,
        siteId: null,
        siteCode: null,
        siteName: null,
        authFlag: null
    }
    proxy.resetForm("enterpriseRef")
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1
    getList()
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef")
    queryParams.value.provinceSiteId = ''
    queryParams.value.citySiteId = ''
    queryParams.value.areaSiteId = ''
    handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id)
    single.value = selection.length != 1
    multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
    reset()
    open.value = true
    title.value = "添加企业管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
    proxy.$router.push({ name: 'enterpriseAduit', query: { id: row.id } })
}
function handleView(row) {
    proxy.$router.push({ name: 'enterpriseDetail', query: { id: row.id } })
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["enterpriseRef"].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateEnterprise(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功")
                    open.value = false
                    getList()
                })
            } else {
                addEnterprise(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功")
                    open.value = false
                    getList()
                })
            }
        }
    })
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value
    proxy.$modal.confirm('是否确认删除企业管理编号为"' + _ids + '"的数据项？').then(function () {
        return delEnterprise(_ids)
    }).then(() => {
        getList()
        proxy.$modal.msgSuccess("删除成功")
    }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('manage/enterprise/export', {
        ...queryParams.value
    }, `enterprise_${new Date().getTime()}.xlsx`)
}

getList()
</script>